package com.wframe.project.jnd.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.former.entity.ProductsAttrvalBean;
import com.wframe.project.jnd.dao.PurchaseTempDao;
import com.wframe.project.jnd.services.PurchaseTempService;
import com.wframe.project.jnd.vo.PriceBeanVo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class PurchaseTempServiceImpl implements PurchaseTempService {

    @Autowired
    PurchaseTempDao purchaseTempDao;

    public PageResult queryList(String userid, int size, int pageno) {
        return purchaseTempDao.queryList(userid, size, pageno);
    }

    public PageResult queryPlanProduct(String panid, String like, int size, int pageno) {
        return purchaseTempDao.queryPlanProduct(panid, like, size, pageno);
    }

    public String saveBuyplan(JdataBean bean) throws Exception {
        return purchaseTempDao.saveBuyplan(bean);
    }

    public void delBuyplan(String userid, String id) {
        purchaseTempDao.delBuyplan(userid, id);
        purchaseTempDao.delBuyplanProduct(userid, id);
    }

    public JdataBean getBuyplan(String userid, String id) {
        return purchaseTempDao.getBuyplan(userid, id);
    }

    public void addBuyplanProduct(String userid, String panid, List<PriceBeanVo> prices) {
        purchaseTempDao.addBuyplanProduct(userid, panid, prices);
    }

    public void delBuyplanProduct(String userid, String panid, String priceid) {
        purchaseTempDao.delBuyplanProduct(userid, panid, priceid);
    }

    public PageResult queryWebProduct(String like, int size, int pageno) {
        PageResult pr = purchaseTempDao.queryWebProduct(like, size, pageno);
        List<Map> list = pr.getList();
        for (Map product : list) {
            int amount = Integer.parseInt(String.valueOf(product.get("realamount")));
            amount = amount <= 0 ? 0 : amount;
            product.put("amount", amount);
            String photo = (product.get("photoview") == null ? "" : product.get("photoview").toString());
            if (StringUtils.isBlank(photo))
                photo = (product.get("product_photoview") == null ? "" : product.get("product_photoview").toString());
            product.put("photo", photo);
            String attrjson = (product.get("attrvals") == null ? "" : product.get("attrvals").toString());
            if (StringUtils.isNotBlank(attrjson)) {
                //将商品属性取出
                List<ProductsAttrvalBean> jsonarr = JSON.parseArray(attrjson, ProductsAttrvalBean.class);
                for (ProductsAttrvalBean attrbean : jsonarr) {
                    product.put(attrbean.getAttrcode(), attrbean.getValtxt());
                }
            }
        }
        return pr;
    }

    public void delRemin(String panid) {
        purchaseTempDao.delRemin(panid);
    }

    @Override
    public Object savePurchaseTemplate(Map map) {
        return purchaseTempDao.savePurchaseTemplate(map);
    }

    @Override
    public PageResult findTemplateProducts(String id, int pageSize, int pageNo) {
        return purchaseTempDao.findTemplateProducts(id, pageSize, pageNo);
    }

    @Override
    public PageResult findTemplateProducts(String userid, String id, int pageSize, int pageNo) {
        return purchaseTempDao.findTemplateProducts(userid, id, pageSize, pageNo);
    }

    @Override
    public List<JdataBean> findPlan() {
        return purchaseTempDao.findPlan();
    }

    @Override
    public PageResult findWebProducts(String userId, String like, int pageSize, int pageNo) {
        PageResult pr = purchaseTempDao.findWebProducts(userId, like, pageSize, pageNo);
        List<Map> list = pr.getList();
        for (Map product : list) {
            int amount = Integer.parseInt(String.valueOf(product.get("realamount")));
            amount = amount <= 0 ? 0 : amount;
            product.put("amount", amount);
            String photo = (product.get("photoview") == null ? "" : product.get("photoview").toString());
            if (StringUtils.isBlank(photo))
                photo = (product.get("product_photoview") == null ? "" : product.get("product_photoview").toString());
            product.put("photo", photo);
            String attrjson = (product.get("attrvals") == null ? "" : product.get("attrvals").toString());
            if (StringUtils.isNotBlank(attrjson)) {
                //将商品属性取出
                List<ProductsAttrvalBean> jsonarr = JSON.parseArray(attrjson, ProductsAttrvalBean.class);
                for (ProductsAttrvalBean attrbean : jsonarr) {
                    product.put(attrbean.getAttrcode(), attrbean.getValtxt());
                }
            }
        }
        return pr;
    }

    @Override
    @Transactional
    public void updateById(Map columnMap, Object id) {
        Map keyMap = new HashMap();
        keyMap.put("id", id);
        try {
            purchaseTempDao.update("wb_buyplan", columnMap, keyMap);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    @Transactional
    public void updateBuyplanProduct(PriceBeanVo bean) {
        purchaseTempDao.update(bean);
    }

    @Override
    public boolean checkOwned(String panid, Object productid) {
        return purchaseTempDao.checkOwned(panid,productid);
    }

}
